Carlisle Rainey
September 22, 2015
nominate.csv data in the data subdirectory of pols-209.pols-209.# load data
nominate <- read.csv("data/nominate.csv")
# quick look at data
library(dplyr)
glimpse(nominate)## Observations: 6,173
## Variables: 6
## $ congress (int) 100, 100, 100, 100, 100, 100, 100, 100,...
## $ state (fctr) USA, ALABAMA, ALABAMA, ALABAMA, ALABAM...
## $ congressional_district (int) 0, 1, 2, 3, 4, 5, 6, 7, 1, 1, 2, 3, 4, ...
## $ party (fctr) Republican, Republican, Republican, De...
## $ name (fctr) REAGAN, CALLAHAN, DICKINSON, NICHOLS ...
## $ ideology_score (dbl) 0.747, 0.358, 0.349, -0.039, -0.203, -0...
# calculate mean
mean(nominate$ideology_score)## [1] 0.08724688
# step-by-step
rep <- nominate$party == "Republican" # logical vector; TRUE for Rs
head(rep) # show first six## [1] TRUE TRUE TRUE FALSE FALSE FALSE
ideology_score_reps <- nominate$ideology_score[rep] # scores for Rs
head(ideology_score_reps) # show first six## [1] 0.747 0.358 0.349 0.253 0.392 0.746
mean(ideology_score_reps) # mean for Rs## [1] 0.5454887
# all at once
mean(nominate$ideology_score[nominate$party == "Republican"]) # Rs## [1] 0.5454887
mean(nominate$ideology_score[nominate$party == "Democrat"]) # Rs## [1] -0.3351859
# all at once
mean(nominate$ideology_score[nominate$party == "Republican" & nominate$congress == 100]) # Rs, 100th## [1] 0.3165556
mean(nominate$ideology_score[nominate$party == "Republican" & nominate$congress == 113]) # Rs, 113th## [1] 0.7270549
mean(nominate$ideology_score[nominate$party == "Democrat" & nominate$congress == 100]) # Rs, 100th## [1] -0.2997308
mean(nominate$ideology_score[nominate$party == "Democrat" & nominate$congress == 113]) # Rs, 113th## [1] -0.3721133
# all at once
sd(nominate$ideology_score[nominate$party == "Republican" & nominate$congress == 100]) # Rs, 100th## [1] 0.1712619
sd(nominate$ideology_score[nominate$party == "Republican" & nominate$congress == 113]) # Rs, 113th## [1] 0.1762492
sd(nominate$ideology_score[nominate$party == "Democrat" & nominate$congress == 100]) # Rs, 100th## [1] 0.1596674
sd(nominate$ideology_score[nominate$party == "Democrat" & nominate$congress == 113]) # Rs, 113th## [1] 0.1130054